/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var classifycategory= 0;


function testclassify(){
    var value = new Array();
    //    value[0]="\\\'(126.5-151.6]\\\'";
    //    value[1]="\\\'(106.6-130.5]\\\'";
    //    value[2]="\\\'All\\\'";
    //    value[3]="\\\'(-inf-0.011111]\\\'";
    //    value[4]="\\\'(-inf-0.022222]\\\'";
    //    value[5]="\\\'(-inf-2.55]\\\'";
    //    value[6]="\\\'(-inf-57.29964]\\\'";
    //    value[7]="\\\'(-inf-4.472223]\\\'";
    //    value[8]="\\\'(-inf-138.63292]\\\'";
    //    value[9]="\\\'(-inf-14.344444]\\\'";
    //    value[10]="\\\'(-inf-13.688889]\\\'";
    //    value[11]="\\\'(-inf-15.088889]\\\'";
    //    value[12]="\\\'(-inf-14.255556]\\\'";
    //    value[13]="\\\'(0.377778-inf)\\\'";
    //    value[14]="\\\'(-0.822222-8.022222]\\\'";
    //    value[15]="\\\'(-9.577777--4.333332]\\\'";
    //    value[16]="\\\'(-inf-15.088889]\\\'";
    //    value[17]="\\\'(0.5-0.6]\\\'";
    //    value[18]="\\\'(-1.451774--0.912186]\\\'";
    
    value[0]="'(126.5-151.6]'";
    value[1]="'(106.6-130.5]'";
    value[2]="'All'";
    value[3]="'(-inf-0.011111]'";
    value[4]="'(-inf-0.022222]'";
    value[5]="'(-inf-2.55]'";
    value[6]="'(-inf-57.29964]'";
    value[7]="'(-inf-4.472223]'";
    value[8]="'(-inf-138.63292]'";
    value[9]="'(-inf-14.344444]'";
    value[10]="'(-inf-13.688889]'";
    value[11]="'(-inf-15.088889]'";
    value[12]="'(-inf-14.255556]'";
    value[13]="'(0.377778-inf)'";
    value[14]="'(-0.822222-8.022222]'";
    value[15]="'(-9.577777--4.333332]'";
    value[16]="'(-inf-15.088889]'";
    value[17]="'(0.5-0.6]'";
    value[18]="'(-1.451774--0.912186]'";
    
    document.getElementsByName('rcc')[0].value = value[0];
    document.getElementsByName('rcr')[0].value= value[1];
    document.getElementsByName('rpc')[0].value= value[2];
    document.getElementsByName('sld5')[0].value= value[3];
    document.getElementsByName('sld2')[0].value= value[4];
    document.getElementsByName('vm')[0].value= value[5];
    document.getElementsByName('vsd')[0].value= value[6];
    document.getElementsByName('hm')[0].value= value[7];
    document.getElementsByName('hsd')[0].value= value[8];
    document.getElementsByName('im')[0].value= value[9];
    document.getElementsByName('rrm')[0].value= value[10];
    document.getElementsByName('rbm')[0].value= value[11];
    document.getElementsByName('rgm')[0].value= value[12];
    document.getElementsByName('erm')[0].value= value[13];
    document.getElementsByName('ebm')[0].value= value[14];
    document.getElementsByName('egm')[0].value= value[15];
    document.getElementsByName('vam')[0].value= value[16];
    document.getElementsByName('sm')[0].value= value[17];
    document.getElementsByName('hm')[1].value= value[18];     
    
    
}

function getCookie(c_name)
{
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name)
        {
            return unescape(y);
        }
    }
}

function uploadTest(){
    document.getElementById('uploadtest').target="target-iframe";           
}


function selectAttrAll() {
    var checkbox = document.forms["filters"]["attr"];
    for (var i = 0; i < checkbox.length; i++){
        checkbox[i].checked = "checked";
    }
}

function deselectAttrAll() {
    var checkbox = document.forms["filters"]["attr"];
    for (var i = 0; i < checkbox.length; i++){
        checkbox[i].checked = "";
    }
}

function showKageKNN() {
    $("#kage-class").slideToggle("slow");    
}

function showWekaKNN() {
    $("#weka-class").slideToggle("slow");    
}

function setRadioActive(idx, name) {
    var x = document.getElementsByName(name);
    x[idx].checked = 'checked';
}

function closeParams() {
    $("#parameters").slideUp("slow");
}

function showParams(type) {
    $("#parameters").slideDown("slow");
    var params = document.getElementById("parameters");
    if (type === 1) {
        params.innerHTML = "<div class='title'>Parameters</div><form id='uploadtest' method='post' action='uploadDataTest.jsp' enctype='multipart/form-data'><div class='inline browser'><input type='file' size='8' name='Data Test'></div><div class='inline browser'><input type='submit' name='' value='OK' onclick='uploadTest()'></div>   <div class='inline' id='statusdatatest'></div></form><br/>";
    } else if (type === 2) {
        params.innerHTML = "<div class='title'>Parameters</div><label>Num of folds: </label><div class='inline'><input class='fold' type='text' size='2' name='folds' placeholder='folds' value='10'/></div><label>folds</label>";
    } else if (type === 3) {
        params.innerHTML = "<div class='title'>Parameters</div><label>Percentage: </label><div class='inline'><input class='fold' type='text' size='2' name='percents' value='66'/></div><label>%</label>";
    }
}

function upload(){
    // upload invisible
    document.getElementById('uploadform').target="target-iframe";        
}




function downloadModel(model){
    if (model==0){
        // weka
        var options= document.getElementsByName('classify');        
        if (options[0].checked){
            location.href= "src/java/model/image_segmentation_model_weka_id3.model";
        }else if (options[1].checked){
            location.href= "src/java/model/image_segmentation_model_weka_ibk.model";
        }else if (options[2].checked){
            location.href= "src/java/model/image_segmentation_model_weka_naive_bayes.model";
        }
        
    }else{
        var options= document.getElementsByName('classify');
        if (options[3].checked){
            location.href= "src/java/model/image_segmentation_model_si_id3.model";
        }else if (options[4].checked){
            location.href= "src/java/model/image_segmentation_model_si_ibk.model";
        }else if (options[5].checked){
            location.href= "src/java/model/image_segmentation_model_si_naive_bayes.model";
        }
    }
    
}

$(document).ready(function(){
    $("#weka").click(function(){
        $("#weka-class").slideToggle("slow");
        classifycategory= 0;        
        $("#kage-class").slideUp("slow");
    });
    
    $("#kage").click(function(){
        classifycategory= 1;    
        $("#kage-class").slideToggle("slow");
        $("#weka-class").slideUp("slow");
    });
    
    $("#k-knn").click(function(){
        $("#kage-knn").slideToggle("slow");
        $("#weka-knn").slideUp("slow");
    });
    $("#k-id3").click(function(){
        $("#kage-knn").slideUp("slow");
    });
    $("#k-nby").click(function(){
        $("#kage-knn").slideUp("slow");
    });
    
    $("#w-knn").click(function(){
        $("#weka-knn").slideToggle("slow");
        $("#kage-knn").slideUp("slow");
    });
    $("#w-id3").click(function(){
        $("#weka-knn").slideUp("slow");
    });
    $("#w-nby").click(function(){
        $("#weka-knn").slideUp("slow");
    });
    $("#DO").click(function(){
        $("#classify-form").slideToggle("slow");       
        $("#relearnbutton").hide(10);
    });
});

function addToLog(log){
    var logarea = document.getElementById("logarea");
    logarea.value = logarea.value +"\n"+ log;             
}

function doClassify(){           
    var algorithm =0;
    if (classifycategory==0){
        // weka
        var options= document.getElementsByName('classify');        
        if (options[0].checked){
            algorithm= 0;
        }else if (options[1].checked){
            algorithm= 1;
        }else if (options[2].checked){
            algorithm= 2;
        }
        
    }else{
        var options= document.getElementsByName('classify');
        if (options[3].checked){
            algorithm= 3;
        }else if (options[4].checked){
            algorithm= 4;
        }else if (options[5].checked){
            algorithm= 5;
        }
    }
    
    var valid=  true;
    var result = "";    
    var log = "";
    var xmlhttp;    
        
    var rcc = document.getElementsByName('rcc')[0].value;
    var rcr = document.getElementsByName('rcr')[0].value;
    var rpc= document.getElementsByName('rpc')[0].value;
    var sld5= document.getElementsByName('sld5')[0].value;
    var sld2= document.getElementsByName('sld2')[0].value;
    var vm= document.getElementsByName('vm')[0].value;
    var vsd= document.getElementsByName('vsd')[0].value;
    var hm1= document.getElementsByName('hm')[0].value;
    var hsd= document.getElementsByName('hsd')[0].value;
    var im= document.getElementsByName('im')[0].value;
    var rrm= document.getElementsByName('rrm')[0].value;
    var rbm= document.getElementsByName('rbm')[0].value;
    var rgm= document.getElementsByName('rgm')[0].value;
    var erm= document.getElementsByName('erm')[0].value;
    var ebm= document.getElementsByName('ebm')[0].value;
    var egm= document.getElementsByName('egm')[0].value;
    var vam= document.getElementsByName('vam')[0].value;
    var sm= document.getElementsByName('sm')[0].value;
    var hm2= document.getElementsByName('hm')[1].value;        
    if(!rcc){
        rcc="null";
    }
    if(!rcr){
        rcr="null";
    }
    if(!rpc){
        rpc="null";
    }
    if(!sld5){
        sld5="null";
    }
    if(!sld2){
        sld2="null";
    }
    if(!vm){
        vm="null";
    }
    if(!vsd){
        vsd="null";
    }
    if(!hm1){
        hm1="null";
    }
    if(!hsd){
        hsd="null";
    }
    if(!im){
        im="null";
    }
    if(!rrm){
        rrm="null";
    }
    if(!rbm){
        rbm="null";
    }
    if(!rgm){
        rgm="null";
    }
    if(!erm){
        erm="null";
    }
    if(!ebm){
        ebm="null";
    }
    if(!egm){
        egm="null";
    }
    if(!vam){
        vam="null";
    }
    if(!sm){
        sm="null";
    }
    if(!hm2){
        hm2="null";
    }
    
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
	
       
            if(xmlhttp.responseText.indexOf("false", 0) == -1){
                if (valid){                       
                    alert("Classification result "+ result +getCookie("classifyresult"));
                    $("#relearnbutton").show();                                
                }
            }
            else {                
                alert("Error");
            }
             
    
        }
                   
    }    
    $("#relearnbutton").hide();          
    
//    alert("doClassify.jsp?id="+algorithm+"&rcc="+rcc+"&rcr="+rcr+"&rpc="+rpc
//        +"&sld5="+sld5+"&sld2="+sld2+"&vm="+vm+"&vsd="+vsd+"&hm1="+hm1
//        +"&hsd="+hsd
//        +"&im="+im+"&rrm="+rrm+"&rbm="+rbm+"&rgm="+rgm+"&erm="+erm+"&ebm="+ebm
//        +"&egm="+egm+"&vam="+vam+"&sm="+sm+"&hm2="+hm2);
    
    xmlhttp.open("GET","doClassify.jsp?id="+algorithm+"&rcc="+rcc+"&rcr="+rcr+"&rpc="+rpc
        +"&sld5="+sld5+"&sld2="+sld2+"&vm="+vm+"&vsd="+vsd+"&hm1="+hm1
        +"&hsd="+hsd
        +"&im="+im+"&rrm="+rrm+"&rbm="+rbm+"&rgm="+rgm+"&erm="+erm+"&ebm="+ebm
        +"&egm="+egm+"&vam="+vam+"&sm="+sm+"&hm2="+hm2,true);
    xmlhttp.send();
   
    
    
}

function doLearningAgain(){
      
    var xmlhttp;    
            
    var algorithm =0;
    if (classifycategory==0){
        // weka
        var options= document.getElementsByName('classify');        
        if (options[0].checked){
            algorithm= 0;
        }else if (options[1].checked){
            algorithm= 1;
        }else if (options[2].checked){
            algorithm= 2;
        }
        
    }else{
        var options= document.getElementsByName('classify');
        if (options[3].checked){
            algorithm= 3;
        }else if (options[4].checked){
            algorithm= 4;
        }else if (options[5].checked){
            algorithm= 5;
        }
    }            
            
    var rcc = document.getElementsByName('rcc')[0].value;
    var rcr = document.getElementsByName('rcr')[0].value;
    var rpc= document.getElementsByName('rpc')[0].value;
    var sld5= document.getElementsByName('sld5')[0].value;
    var sld2= document.getElementsByName('sld2')[0].value;
    var vm= document.getElementsByName('vm')[0].value;
    var vsd= document.getElementsByName('vsd')[0].value;
    var hm1= document.getElementsByName('hm')[0].value;
    var hsd= document.getElementsByName('hsd')[0].value;
    var im= document.getElementsByName('im')[0].value;
    var rrm= document.getElementsByName('rrm')[0].value;
    var rbm= document.getElementsByName('rbm')[0].value;
    var rgm= document.getElementsByName('rgm')[0].value;
    var erm= document.getElementsByName('erm')[0].value;
    var ebm= document.getElementsByName('ebm')[0].value;
    var egm= document.getElementsByName('egm')[0].value;
    var vam= document.getElementsByName('vam')[0].value;
    var sm= document.getElementsByName('sm')[0].value;
    var hm2= document.getElementsByName('hm')[1].value;        
    var result = getCookie('classifyresult');
    if(!rcc){
        rcc="null";
    }
    if(!rcr){
        rcr="null";
    }
    if(!rpc){
        rpc="null";
    }
    if(!sld5){
        sld5="null";
    }
    if(!sld2){
        sld2="null";
    }
    if(!vm){
        vm="null";
    }
    if(!vsd){
        vsd="null";
    }
    if(!hm1){
        hm1="null";
    }
    if(!hsd){
        hsd="null";
    }
    if(!im){
        im="null";
    }
    if(!rrm){
        rrm="null";
    }
    if(!rbm){
        rbm="null";
    }
    if(!rgm){
        rgm="null";
    }
    if(!erm){
        erm="null";
    }
    if(!ebm){
        ebm="null";
    }
    if(!egm){
        egm="null";
    }
    if(!vam){
        vam="null";
    }
    if(!sm){
        sm="null";
    }
    if(!hm2){
        hm2="null";
    }    
    if (!result){
        result="";
    }
    
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
	
            if(xmlhttp.responseText.indexOf("false", 0) == -1){
                alert("Relearn success");
                document.location.href = "index.jsp";
            }
            else {                
                alert("Error");
            } 
        }
                   
    }        
    
//    alert("doRelearn.jsp?id="+algorithm+"&rcc="+rcc+"&rcr="+rcr+"&rpc="+rpc
//        +"&sld5="+sld5+"&sld2="+sld2+"&vm="+vm+"&vsd="+vsd+"&hm1="+hm1
//        +"&hsd="+hsd
//        +"&im="+im+"&rrm="+rrm+"&rbm="+rbm+"&rgm="+rgm+"&erm="+erm+"&ebm="+ebm
//        +"&egm="+egm+"&vam="+vam+"&sm="+sm+"&hm2="+hm2);
    
    xmlhttp.open("GET","doRelearn.jsp?id="+algorithm+"&rcc="+rcc+"&rpc="+rpc
        +"&sld5="+sld5+"&sld2="+sld2+"&vm="+vm+"&vsd="+vsd+"&hm1="+hm1
        +"&hsd="+hsd
        +"&im="+im+"&rrm="+rrm+"&rbm="+rbm+"&rgm="+rgm+"&erm="+erm+"&ebm="+ebm
        +"&egm="+egm+"&vam="+vam+"&sm="+sm+"&hm2="+hm2,true);
    xmlhttp.send();    
    
}

function visualizeWekaID3(){
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
	
            if(xmlhttp.responseText.indexOf("false", 0) == -1){
                alert("Visualization Weka ID3\n"+xmlhttp.responseText);
            }
            else {                
                alert("Error");
            } 
        }
                   
    }    
    xmlhttp.open("GET","visualizeID3.jsp?id=0",true);
    xmlhttp.send();
    
}

function visualizeKageID3(){
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
	
            if(xmlhttp.responseText.indexOf("false", 0) == -1){
                    alert("Visualization Kage ID3\n"+xmlhttp.responseText);
            }
            else {                
                alert("Error");
            } 
        }
                   
    }    
    xmlhttp.open("GET","visualizeID3.jsp?id=1",true);
    xmlhttp.send();
    
}
